package cz.byteworks.endownload;

import java.io.FileWriter;

import cz.byteworks.exportino.IRecord;
import cz.byteworks.exportino.IRecordProcessor;
import cz.byteworks.exportino.ProcessingException;

public class ActivityProcessor implements IRecordProcessor {
	
	private String exportDir;
	
	public ActivityProcessor(String exportDir) {
		if (exportDir == null) {
			throw new IllegalArgumentException("Export directory is empty");
		}
		this.exportDir = exportDir;
		if (!this.exportDir.endsWith("/") && !this.exportDir.endsWith("\\")) {
			this.exportDir = this.exportDir + "/";
		}
		
	}

	@Override
	public void processRecord(IRecord record) throws ProcessingException {
		try {
			Activity activity = (Activity)record;
			String fileName = activity.getId() + "." + activity.getType();
			FileWriter writer = new FileWriter(exportDir + fileName);
			writer.write(activity.getData());
			writer.close();
		} catch (Exception e) {
			throw new ProcessingException(e.getMessage());
		}
	}

}
